# -*- coding:utf-8 -*-
import requests
from bs4 import BeautifulSoup


class JwxtSpider:

    def __init__(self):
        self.session = requests.session()
        self.login_data = {'zjh': "", 'mm': ""}
        self.login_url = "http://jwxt.imu.edu.cn/loginAction.do"
        self.search_url = "http://jwxt.imu.edu.cn:80/setReportParams"
        self.search_data = {'LS_XH': "", 'resultPage': "http://jwxt.imu.edu.cn:80/reportFiles/cj/cj_zwcjd.jsp?"}

    def login(self, zjh, mm):
        self.login_data['zjh'] = zjh
        self.login_data['mm'] = mm
        self.session.post(self.login_url, self.login_data)
        print "Login Success!"

    def get_grade_page1(self, no):
        self.search_data['LS_XH'] = no
        self.grade_page1 = self.session.post(self.search_url, self.search_data)
        return self.grade_page1.text

    def get_grade_page2(self):
        page1_tag = BeautifulSoup(self.grade_page1.text, "lxml")
        turn_page_form = page1_tag.find(name="form", attrs={"name": "report1_turnPageForm"})
        input_tag_list = turn_page_form.find_all("input")
        form_data = {}
        for input_tag in input_tag_list:
            key = input_tag['name']
            value = input_tag['value']
            form_data[key] = value
        form_data['report1_currPage'] = 2
        print form_data
        form_url = turn_page_form.attrs['action']
        self.grade_page2 = self.session.post(form_url, form_data)
        return self.grade_page2.text

    def parse(self):
        pass

if __name__ == '__main__':
    spider = JwxtSpider()
    spider.login("0141124920", "")
    print spider.get_grade_page1("0141124920")
    print spider.get_grade_page2()
    print "*****************************"
    print spider.get_grade_page1("0141124655")
    print spider.get_grade_page2()
